package com.cpi.cornpp.product.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;

import com.cpi.cornpp.product.dto.ProductDTO;
import com.cpi.cornpp.product.view.ProductView;
import com.cs.common.utils.StringUtils;

public class ProductConverter {

	public final ProductView convert(ProductDTO productDTO){
		ProductView productView = null;
		
		if (productDTO != null){
			productView = new ProductView();
			
			productView.setProductId(productDTO.getProductId().trim());
			productView.setDescription(productDTO.getDescription().trim());
		}
		
		return productView;
	}
	
	public final ProductDTO convert(ProductView productView){
		ProductDTO productDTO = null;
		StringBuffer completeDescription = null;
		
		completeDescription = new StringBuffer(StringUtils.PERCENT_CHAR);
		if (productView != null){
			productDTO = new ProductDTO();
			productDTO.setProductId(productView.getProductId());			
			productDTO.setDescription(completeDescription.append(productView.getDescription()).append(StringUtils.PERCENT_CHAR).toString());
		}
		
		return productDTO;
	}
	
	public final List convertViews(List productViews){
		List products = null;
		
		products = new ArrayList();
		
		if (!CollectionUtils.isEmpty(productViews)){
			Iterator iterator = productViews.iterator();
			ProductView view = null;
			while(iterator.hasNext()){
				view = (ProductView)iterator.next();
				products.add(convert(view));
			}
		}
		
		return products;
	}
	
	public final List convertDTOs(List productDTO){
		List products = null;
		
		products = new ArrayList(0);
		
		if (!CollectionUtils.isEmpty(productDTO)){
			Iterator iterator = productDTO.iterator();
			ProductDTO dto = null;
			while(iterator.hasNext()){
				dto = (ProductDTO)iterator.next();
				products.add(convert(dto));
			}
		}
		
		return products;
	}
}
